package com.ruoyi.sports.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sports.mapper.ScoreMapper;
import com.ruoyi.sports.domain.Score;
import com.ruoyi.sports.service.IScoreService;

import javax.validation.Validator;

/**
 * 参赛成绩Service业务层处理
 * 
 * @author ZJH
 * @date 2023-03-08
 */
@Service
public class ScoreServiceImpl implements IScoreService 
{
    @Autowired
    private ScoreMapper scoreMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询参赛成绩
     * 
     * @param scoreId 参赛成绩主键
     * @return 参赛成绩
     */
    @Override
    public Score selectScoreByScoreId(Long scoreId)
    {
        return scoreMapper.selectScoreByScoreId(scoreId);
    }

    /**
     * 查询参赛成绩列表
     * 
     * @param score 参赛成绩
     * @return 参赛成绩
     */
    @Override
    public List<Score> selectScoreList(Score score)
    {
        return scoreMapper.selectScoreList(score);
    }

    /**
     * 新增参赛成绩
     * 
     * @param score 参赛成绩
     * @return 结果
     */
    @Override
    public int insertScore(Score score)
    {
        return scoreMapper.insertScore(score);
    }

    /**
     * 修改参赛成绩
     * 
     * @param score 参赛成绩
     * @return 结果
     */
    @Override
    public int updateScore(Score score)
    {
        return scoreMapper.updateScore(score);
    }

    /**
     * 批量删除参赛成绩
     * 
     * @param scoreIds 需要删除的参赛成绩主键
     * @return 结果
     */
    @Override
    public int deleteScoreByScoreIds(Long[] scoreIds)
    {
        return scoreMapper.deleteScoreByScoreIds(scoreIds);
    }

    /**
     * 删除参赛成绩信息
     * 
     * @param scoreId 参赛成绩主键
     * @return 结果
     */
    @Override
    public int deleteScoreByScoreId(Long scoreId)
    {
        return scoreMapper.deleteScoreByScoreId(scoreId);
    }

    /**
     * 导入参赛成绩数据
     *
     * @param scoreList     用户数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName      操作用户
     * @return 结果
     */
    @Override
    public String importScore(List<Score> scoreList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(scoreList) || scoreList.size() == 0)
        {
            throw new ServiceException("导入参赛成绩数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Score score : scoreList)
        {
            try
            {
                // 验证是否存在这个成绩
                Score s = scoreMapper.selectScoreByScoreId(score.getScoreId());
                if (StringUtils.isNull(s))
                {
                    BeanValidators.validateWithException(validator, score);

                    this.insertScore(score);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、成绩 " +score+" 导入成功");
                }
                else if (updateSupport)
                {
                    BeanValidators.validateWithException(validator, score);
                    this.updateScore(score);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、成绩 " +score + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、成绩 " + score + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、成绩 " + score + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<Score> selectMyScoreList(Score score) {
        score.setUserId(SecurityUtils.getLoginUser().getUserId());
        return scoreMapper.selectScoreList(score);
    }
}
